package com.draemoncode.socket;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

public class MinaClientMain {

	public static void main(String[] args) {
		// 创建客户端连接器.
				NioSocketConnector connector = new NioSocketConnector();
				connector.getFilterChain().addLast("logger", new LoggingFilter());
				connector.getFilterChain().addLast("codec",
						new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));

				// 设置连接超时检查时间
				connector.setConnectTimeoutCheckInterval(30);
				connector.setHandler(new MinaClientHandlerAdapter());
				// 建立连接
				ConnectFuture cf = connector.connect(new InetSocketAddress("localhost", 7100));
				// 等待连接创建完成
				cf.awaitUninterruptibly();
				String json = "{\"money\":3000,\"name\":\"hqz\",\"type\":1,\"syncid\":0}";
				cf.getSession().write(json);

				// 等待连接断开
				cf.getSession().getCloseFuture().awaitUninterruptibly();
				// 释放连接
				connector.dispose();
	}
}
